package com.ex1;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipeExample {
    public static void main(String[] args) throws IOException {
        PipedInputStream in = new PipedInputStream();
        PipedOutputStream out = new PipedOutputStream();
        in.connect(out);

        Thread t1 = new Thread(() -> {
            try {
                String message = "Hello, world!";
                out.write(message.getBytes());
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                byte[] buffer = new byte[1024];
                int len = in.read(buffer);
                String message = new String(buffer, 0, len);
                System.out.println(message);
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        t1.start();
        t2.start();
    }
}
